package liuh.com.ledlamp.handle;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.blankj.utilcode.utils.ToastUtils;
import com.zhy.http.okhttp.builder.PostStringBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.net.URLEncoder;

import liuh.com.ledlamp.adapter.NormalRecyclerViewAdapter;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author :liuh
 * @note 显示内容；撤销内容网络请求处理
 * @date: 2016-12-05
 */

public class DisHandle {

    private static final String TAG = "DisHandle";
    Context mContext;
    NormalRecyclerViewAdapter mRecyclerViewAdapter;
    ProgressDialog mProgressDialog;
    int mPosition;
    String title_t, wid_t = "";

    public DisHandle(Context context,NormalRecyclerViewAdapter RecyclerViewAdapter,
                     int position,ProgressDialog progressDialog) {
        mContext = context;
        mRecyclerViewAdapter=RecyclerViewAdapter;
        mPosition=position;
        mProgressDialog=progressDialog;
    }

    /**
     * @note 内容显示到led屏幕
     * @author liuh
     */
    public void disContent(PostStringBuilder builder, String title, String wid) throws Exception {
        title_t = title;
        wid_t = wid;
        String content_str = URLEncoder.encode(title, "gbk");
        StringBuilder sBuilder=new StringBuilder();
        sBuilder.append("web_id=8&txt_delay=1500&sel_exit=0&txt_speed2=1&txt_times=65535&sel_text_font=3&sel_text_color=0&sel_text_ver=1");
        if(title.length()>8){
            sBuilder.append("&sel_enter=2");
            sBuilder.append("&txt_speed=2");
            sBuilder.append("&sel_text_hor=0");
        }else{
            sBuilder.append("&sel_enter=1");
            sBuilder.append("&txt_speed=1");
            sBuilder.append("&sel_text_hor=1");
        }
        sBuilder.append("&text_wid=" + wid);
        sBuilder.append("&txt_content=" + content_str);
        builder.url("http://192.168.16.254/web/list.cgi")
                .mediaType(MediaType.parse("application/x-www-form-urlencoded"))
                //.content("web_id=8&text_wid=" + wid + "&sel_enter=1&txt_speed=1&txt_delay=1200&sel_exit=0&txt_speed2=1&txt_times=65535&sel_text_font=2&sel_text_hor=1&sel_text_color=0&sel_text_ver=1&txt_content=" + content_str)
                .content(sBuilder.toString())
                .id(1)
                .build()
                .execute(new MyStringCallback());
    }

    /**
     * @note 撤销内容显示到led屏幕
     * @author liuh
     */
    public void unDisContent(PostStringBuilder builder, String wid) throws Exception {
        wid_t = wid;
        builder.url("http://192.168.16.254/web/list.cgi")
                .mediaType(MediaType.parse("application/x-www-form-urlencoded"))
                .content("web_id=7&hid_player_del=" + wid)
                .id(2)
                .build()
                .execute(new MyStringCallback());
    }

    public class MyStringCallback extends StringCallback {
        @Override
        public void onBefore(Request request, int id) {
            //Log.d(TAG, "onBefore：complete");
            mProgressDialog.show();
        }

        @Override
        public void onAfter(int id) {
            //Log.d(TAG, "onAfter：complete");
            mProgressDialog.dismiss();
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            e.printStackTrace();
            Log.d(TAG, "onError：complete");
            switch (id){
                case 1:
                    DisTitle.getInstance().wids.add(wid_t);
                    Log.d(TAG, "显示内容失败:"+title_t+"---wid"+wid_t);
                    break;
                case 2:
                    Log.d(TAG, "撤销内容失败:---wid"+wid_t);
                    break;
            }
            ToastUtils.showLongToast(mContext, "操作失败！");
        }

        @Override
        public void onResponse(String response, int id) {
            Log.d(TAG, "onResponse：complete");
            try {
                switch (id) {
                    case 1:
                        // 显示内容
                        Log.d(TAG, "显示内容:"+title_t+"---wid"+wid_t);
                        DisTitle.getInstance().disWid(title_t, wid_t, mContext);
                        break;
                    case 2:
                        // 撤销内容
                        Log.d(TAG, "撤销内容:---wid"+wid_t);
                        DisTitle.getInstance().unDisWid(wid_t, mContext);
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 刷新RecyclerView
            mRecyclerViewAdapter.notifyItemChanged(mPosition,1);
            ToastUtils.showLongToast(mContext, "操作成功!");
        }

        @Override
        public void inProgress(float progress, long total, int id) {
            Log.e(TAG, "inProgress:" + progress);
        }

        @Override
        public String parseNetworkResponse(Response response, int id) throws IOException
        {
            return response.code()+"";
        }
    }
}
